package com.bookmanagersystem.controller;

import com.bookmanagersystem.dao.UserDao;
import com.bookmanagersystem.dao.UserDaoImpl;
import com.bookmanagersystem.entity.User;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.List;

@WebServlet("/user/updateUser")
public class UpdateUserServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request,
                           HttpServletResponse response) throws ServletException, IOException {

        String id = request.getParameter("id");
        String userName = request.getParameter("username");
        String pwd = request.getParameter("pwd");
        String name = request.getParameter("name");
        String age = request.getParameter("age");

        UserDao userDao = new UserDaoImpl();

        User user = new User();
        user.setId(Integer.parseInt(id));
        user.setUserName(userName);
        user.setPassword(pwd);
        user.setName(name);
        user.setAge(Integer.parseInt(age));


        int i = userDao.updateUser(user);
        if (i > 0) {
            List<User> users = userDao.selectAllUsers();

            request.getSession().setAttribute("users", users);
            response.sendRedirect("/book/user/list.jsp");
        }


    }
}
